//
// Created by LONG on 2020/11/7.
//

#include "l1Api.h"

void RAN_SCF_L1_API_ProcParamsReq(S_RanCommMsgHdr *pCommMsgHdr)
{
    int32_t rc;
    S_VarRingBuffer *pL2L1CpMsgQue;
    S_RanL1ApiMsgParamsReq *pParamsReq;

    pParamsReq = (S_RanL1ApiMsgParamsReq *)pCommMsgHdr;
    pL2L1CpMsgQue = gpRanL2Context->nbInst[pParamsReq->nbIdx].cellInst[pParamsReq->cellIdx].pL2L1CpMsgQue;

    VRB_PUT(rc, pL2L1CpMsgQue, pCommMsgHdr, pCommMsgHdr->size);
}

void RAN_SCF_L1_API_ProcConfigReq(S_RanCommMsgHdr *pCommMsgHdr)
{
    int32_t rc;
    S_VarRingBuffer *pL2L1CpMsgQue;
    S_RanL1ApiMsgConfigReq *pConfigReq;

    pConfigReq = (S_RanL1ApiMsgConfigReq *)pCommMsgHdr;
    pL2L1CpMsgQue = gpRanL2Context->nbInst[pConfigReq->nbIdx].cellInst[pConfigReq->cellIdx].pL2L1CpMsgQue;

    VRB_PUT(rc, pL2L1CpMsgQue, pCommMsgHdr, pCommMsgHdr->size);
}

void RAN_SCF_L1_API_ProcStartReq(S_RanCommMsgHdr *pCommMsgHdr)
{
    int32_t rc;
    S_VarRingBuffer *pL2L1CpMsgQue;
    S_RanL1ApiMsgStartReq *pStartReq;

    pStartReq = (S_RanL1ApiMsgStartReq *)pCommMsgHdr;
    pL2L1CpMsgQue = gpRanL2Context->nbInst[pStartReq->nbIdx].cellInst[pStartReq->cellIdx].pL2L1CpMsgQue;

    VRB_PUT(rc, pL2L1CpMsgQue, pCommMsgHdr, pCommMsgHdr->size);
}

void RAN_SCF_L1_API_TtiProc(S_RanL2CellInst *pCellInst, uint32_t schRsltIdx)
{
    int32_t rc;
    S_RanSchRslt *pSchRslt;
    S_RanL1ApiDlTtiReqPdu *pdu;
    S_RanL1ApiMsgDlTtiReq dlTtiReq;

    pSchRslt = &pCellInst->schRslt[schRsltIdx];

    dlTtiReq.commMsgHdr.type = RAN_L1_API_MSG_DL_TTI_REQ;
    dlTtiReq.commMsgHdr.size = sizeof(dlTtiReq);
    dlTtiReq.nbIdx = pCellInst->nbIdx;
    dlTtiReq.cellIdx = pCellInst->cellIdx;
    dlTtiReq.sfn = pSchRslt->sfnSlot.sfn;
    dlTtiReq.slot = pSchRslt->sfnSlot.slot;

    dlTtiReq.pduNum = 0;

    if (pSchRslt->bchExist)
    {
        pdu = &dlTtiReq.pdus[dlTtiReq.pduNum];

        pdu->pduType = RAN_L1_API_DL_TTI_PDU_TYPE_SSB;
        pdu->pduSize = sizeof(S_RanL1ApiDlSsbPdu);
        pdu->pdu.ssbPdu.bchPayload = pSchRslt->bchSchRslt.bchPayload;

        dlTtiReq.pduNum++;
    }

    VRB_PUT(rc, pCellInst->pL2L1UpMsgQue, &dlTtiReq, sizeof(dlTtiReq));
}
